import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {MdlDialogService, MdlSnackbarService} from '@angular-mdl/core';
import {CustomService} from '../../custom.service';
import {ConfirmationService} from 'primeng/api';
import { FbDynamicFormComponent } from '../../../form/design/drawer/fb-dynamic-form.component';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'custom-fields',
    templateUrl: './fields.component.html',
    styleUrls: ['./fields.component.scss'],
    providers: [CustomService]
})
export class FieldsComponent implements OnInit {
    @Input() data: any;
    currentItemId = 0;
    recId: number = 0;
    formIdValue = 0;
    bizIdValue = 0;
    scrollHeight = '450px';
    tableName: string;
    tables: any[] = [];
    fields: any[] = [];
    componentTypes: any[] = [];
    cols: any[] = [
        { header: '序号', field: 'displayOrder', style: { 'width': '80px', 'text-align': 'center', 'font-weight': 600 } },
        { header: '字段名称', field: 'columnName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '显示名称', field: 'displayName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '数据类型', field: 'dataTypeName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '组件类型', field: 'componentType', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } } ,
        { header: '可见标记', field: 'visableFlag', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: '占据行', field: 'occupyRow', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '是否必填', field: 'isRequired', style: { 'text-align': 'center', 'overflow': 'hidden' } },
    ];

    @ViewChild(FbDynamicFormComponent, { static: true })
    private fbDynamicFormComponent: FbDynamicFormComponent;

    @Input()
    set itemId(itemId: number) {
        if (itemId) {
            this.currentItemId = itemId;
            this.customService.loadCustomTableFields(this.currentItemId).subscribe(fields => this.fields = fields);
        }
    }
    get itemId(): number { return this.currentItemId; }
    @Input()
    set formId(formId: number) {
        if (!formId) { return; }
        this.formIdValue = formId;
    }
    get formId(): number { return this.formIdValue; }

    @Input()
    set bizId(bizId: number) {
        if (!bizId) { return; }
        this.bizIdValue = bizId;
    }
    get bizId(): number { return this.bizIdValue; }


    constructor(
        private route: ActivatedRoute,
        private customService: CustomService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) {
        this.recId = +this.route.snapshot.paramMap.get('recId');
    }
    ngOnInit() {
        this.scrollHeight = $(document).height() - 245 + 'px';
        this.customService.loadCustomTables().subscribe(data => {
            let items = [];
            data.forEach(e => items.push({ label: e, value: e }));
            this.tables = items;
        });
        this.customService.loadCustomTableFields(this.currentItemId).subscribe(fields => this.fields = fields);
        this.customService.loadFormAssemblyDef().subscribe(data => {
            let items = [];
            data.forEach(e => items.push({ label: e.name, value: e.type_code }));
            this.componentTypes = items;
        });
    }

    saveForm() {
        this.mdlDialogService.confirm('您正在重新生成表单程序，将覆盖原有表单，是否继续？', '取消', '确定').subscribe(() => {
            let temp = this.fields;
            temp.forEach(e => e.visableFlag = (e.visableFlag ? 1 : 0));
            temp.forEach(e => e.isRequired = (e.isRequired ? 1 : 0));
            // this.fbDynamicFormComponent.onLoadValue(this.recId, 0, this.formIdValue, 0, this.bizIdValue);
            this.customService.saveCustomTableForAssemblies(this.formIdValue, this.fields).subscribe(data => {
                this.mdlSnackbarService.showToast('生成表单成功');
            });
        });
    }
    saveUpdate() {
        let temp = this.fields;
        temp.forEach(e => e.visableFlag = (e.visableFlag ? 1 : 0));
        temp.forEach(e => e.isRequired = (e.isRequired ? 1 : 0));
        this.customService.saveCustomTableFields(this.currentItemId, this.tableName || '', this.fields).subscribe(data => {
            this.mdlSnackbarService.showToast('保存成功');
        });
    }

    onTableChange(event) {
        if (event.value) {
            this.tableName = event.value.value;
            this.customService.loadCustomTableFields(this.currentItemId, this.tableName).subscribe(fields => this.fields = fields);
        } else {
            this.customService.loadCustomTableFields(this.currentItemId).subscribe(fields => this.fields = fields);
        }
    }
}
